package org.spbsu.squaddie.grapher;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Normalizer {

	// public List<Double> deductMinimum(List<Double> array) {
	//
	// if (array == null || array.isEmpty()) {
	// System.out.println("Invalid input.");
	// return Collections.emptyList();
	// }
	//
	// List<Double> result = new ArrayList<>();
	//
	// Double minimum = array.get(0);
	// for (int i = 0; i < array.size(); ++i) {
	// if (array.get(i) < minimum) {
	// minimum = array.get(i);
	// }
	// }
	//
	// for (int i = 0; i < array.size(); ++i) {
	// result.add(array.get(i) - minimum);
	// }
	// }
	public List<Double> deduct(List<Double> array, Double deduction) {

		if (array == null || array.isEmpty()) {
			System.out.println("Invalid input.");
			return Collections.emptyList();
		}

		List<Double> result = new ArrayList<>();

		for (int i = 0; i < array.size(); ++i) {
			result.add(array.get(i) - deduction);
		}

		return result;
	}

	public List<Double> deductFirst(List<Double> array) {
		return deduct(array, array.get(0));
	}

	public List<Double> normalize(List<Double> array, Double min, Double max) {
		List<Double> result = new ArrayList<>();

		for (int i = 0; i < array.size(); ++i) {
			if (array.get(i) > max || array.get(i) < min) {
				System.out.println("Value " + array.get(i) + " is invalid.");
				return Collections.emptyList();
			}
			result.add((array.get(i) - min) / (max - min));
		}

		return result;

	}

	public Double[] denormalize(Double[] array, Double min, Double max) {
		Double[] result = new Double[array.length];

		for (int i = 0; i < array.length; ++i) {
			result[i] = denormalize(array[i], min, max);
		}

		return result;

	}

	public Double denormalize(Double input, Double min, Double max) {
		return input * (max - min) + min;
	}

}
